var CashMethodInfoDlg = {
    cashMethodInfoData : {},
    zTreeInstance : null,
    validateFields: {
        checkoutMethod: {
            validators: {
                notEmpty: {
                    message: '收费方式不能为空'
                },
                stringLength: {
                    max: 20,
                    message: '收费方式长度不能超过20，请重新输入...'
                }/*最后一个没有逗号*/
            }
        },
    }
};

CashMethodInfoDlg.clearData = function() {this.cashMethodInfoData = {};};//清除数据

CashMethodInfoDlg.set = function(key, val) {this.cashMethodInfoData[key] = (typeof value == "undefined") ? $("#" + key).val() : value;return this;};

CashMethodInfoDlg.get = function(key) {return $("#" + key).val();};//设置对话框中的数据 @param key 数据的名称 @param val 数据的具体值

CashMethodInfoDlg.close = function() {parent.layer.close(parent.layer.getFrameIndex(window.name));};//关闭此对话框

/**
 * 显示部门选择的树
 *
 * @returns
 */
CashMethodInfoDlg.showCheckoutMethodSelectTree = function() {
    var pName = $("#pName");
    var pNameOffset = $("#pName").offset();
    $("#parentCheckoutMethodMenu").css({
        left : pNameOffset.left + "px",
        top : pNameOffset.top + pName.outerHeight() + "px"
    }).slideDown("fast");

    $("body").bind("mousedown", onBodyDown);
}

/**
 * 隐藏部门选择的树
 */
CashMethodInfoDlg.hideCheckoutMethodSelectTree = function() {
    $("#parentCheckoutMethodMenu").fadeOut("fast");
    $("body").unbind("mousedown", onBodyDown);// mousedown当鼠标按下就可以触发，不用弹起
}

/**
 * 收集数据
 */
CashMethodInfoDlg.collectData = function() {
    this.set('id').set('checkoutMethod').set('remarks').set('num');
}

/**
 * 验证数据是否为空
 */
CashMethodInfoDlg.validate = function () {
    $('#checkoutMethodInfoForm').data("bootstrapValidator").resetForm();
    $('#checkoutMethodInfoForm').bootstrapValidator('validate');
    return $("#checkoutMethodInfoForm").data('bootstrapValidator').isValid();
}

/**
 * 提交添加
 */
CashMethodInfoDlg.addSubmit = function() {
    this.clearData();
    this.collectData();
    if (!this.validate()) {
        return;
    }
    var checkoutMethod = $('#checkoutMethod').val();
    var remarks = $('#remarks').val();
    var num = $('#num').val();
    if (checkoutMethod != "" && checkoutMethod == "") {
        alert("请输入收费方式！");
        return
    }
    if (remarks != "" && remarks == "") {
        alert("请输入备注！");
        return
    }
    if (num != "" && num == "") {
        alert("请输入排序！");
        return
    }
    var ajax = new $ax(Feng.ctxPath + "/checkoutMethod/cashAdd", function(data){
        Feng.success("添加成功!");
        window.parent.CashDailyMethod.table.refresh();
        CashMethodInfoDlg.close();
    },function(data){
        Feng.error("添加失败!" + data.responseJSON.message + "!");
    });
    ajax.set(this.cashMethodInfoData);
    ajax.start();
}

/**
 * 提交修改
 */
CashMethodInfoDlg.editSubmit = function() {
    this.clearData();
    this.collectData();
    if (!this.validate()) {
        return;
    }
    //提交信息
    var ajax = new $ax(Feng.ctxPath + "/checkoutMethod/cashUpdate", function(data){
        Feng.success("修改成功!");
        window.parent.CashDailyMethod.table.refresh();
        CashMethodInfoDlg.close();
    },function(data){
        Feng.error("修改失败!" + data.responseJSON.message + "!");
    });
    ajax.set(this.cashMethodInfoData);
    ajax.start();
}

function onBodyDown(event) {
    if (!(event.target.id == "menuBtn" || event.target.id == "parentCheckoutMethodMenu" || $(
            event.target).parents("#parentCheckoutMethodMenu").length > 0)) {
        CashMethodInfoDlg.hideCheckoutMethodSelectTree();
    }
}

$(function() {
    Feng.initValidator("checkoutMethodInfoForm", CashMethodInfoDlg.validateFields);
});
